import ProxyInterface from '../proxy-interface';
import {
  FlowGroupModel, FlowGroupPageModel, FlowModel, FlowPageModel, TaskModel,
  TaskPageModel, ProcessStageModel, TaskStageModel, TaskEnclosureModel, TaskTodoModel, TaskCommentModel
} from '../model/task-flow';

// task
class TaskPageProxy implements ProxyInterface<TaskPageModel> {
  private taskPage = new TaskPageModel();

  constructor (responseData) {
    let data = responseData;
    Object.assign<TaskPageModel, any>(this.taskPage, data);
  }

  getModel (): TaskPageModel {
    return this.taskPage;
  }
}

class TaskProxy implements ProxyInterface<TaskModel> {
  private task = new TaskModel();

  constructor (responseData) {
    let data = responseData.data || responseData;
    Object.assign<TaskModel, any>(this.task, data);
  }

  getModel (): TaskModel {
    return this.task;
  }
}

class TaskStageProxy implements ProxyInterface<TaskStageModel> {
  private taskStage = new TaskStageModel();

  constructor (responseData) {
    let data = responseData.data || responseData;
    Object.assign<TaskStageModel, any>(this.taskStage, data);
  }

  getModel (): TaskStageModel {
    return this.taskStage;
  }
}

class TaskEnclosureProxy implements ProxyInterface<TaskEnclosureModel[]> {
  private taskEnclosures = [];

  constructor (responseData) {
    let data = responseData.data || responseData;
    // 这里因为不需要分页，所以没有定义附件的分页model，直接使用data.items获取主要内容
    if (data.items) {
      data.items.map((item) => {
        let taskEnclosure = new TaskEnclosureModel();
        this.taskEnclosures.push(Object.assign<TaskEnclosureModel, any>(taskEnclosure, item));
      });
    }
  }

  getModel (): TaskEnclosureModel[] {
    return this.taskEnclosures;
  }
}

class TaskCommentsProxy implements ProxyInterface<TaskCommentModel[]> {
  private taskComments = [];

  constructor (responseData) {
    let data = responseData.data || responseData;
    // 这里因为不需要分页，所以没有定义附件的分页model，直接使用data.items获取主要内容
    if (data.items) {
      data.items.map((item) => {
        let taskComment = new TaskCommentModel();
        this.taskComments.push(Object.assign<TaskCommentModel, any>(taskComment, item));
      });
    }
  }

  getModel (): TaskCommentModel[] {
    return this.taskComments;
  }
}

class TaskToDoProxy implements ProxyInterface<TaskTodoModel> {
  private taskToDo = new TaskTodoModel();

  constructor (responseData) {
    let data = responseData.data || responseData;
    Object.assign<TaskTodoModel, any>(this.taskToDo, data);
  }

  getModel (): TaskTodoModel {
    return this.taskToDo;
  }
}

// flow
class FlowPageProxy implements ProxyInterface<FlowPageModel> {
  public flowPage = new FlowPageModel();

  constructor (responseData) {
    Object.assign<FlowPageModel, any>(this.flowPage, responseData);
  }

  getModel (): FlowPageModel {
    return this.flowPage;
  }
}

class FlowProxy implements ProxyInterface<FlowModel> {
  public flow = new FlowModel();

  constructor (responseData) {
    Object.assign<FlowModel, any>(this.flow, responseData.data);
  }

  getModel (): FlowModel {
    return this.flow;
  }
}

// stage ProcessStageModel
class FlowStageProxy implements ProxyInterface<ProcessStageModel> {
  public stage = new ProcessStageModel();

  constructor (responseData) {
    Object.assign<ProcessStageModel, any>(this.stage, responseData.data);
  }

  getModel (): ProcessStageModel {
    return this.stage;
  }
}

// group
class FlowGroupListProxy implements ProxyInterface<FlowGroupPageModel> {
  public flowGroupList = new FlowGroupPageModel();

  constructor (responseData) {
    Object.assign<FlowGroupPageModel, any>(this.flowGroupList, responseData);
  }

  getModel (): FlowGroupPageModel {
    return this.flowGroupList;
  }
}

class FlowGroupProxy implements ProxyInterface<FlowGroupModel> {
  public flowGroup = new FlowGroupModel();

  constructor (responseData) {
    Object.assign<FlowGroupModel, any>(this.flowGroup, responseData.data);
  }

  getModel (): FlowGroupModel {
    return this.flowGroup;
  }
}

export {
  TaskPageProxy,
  TaskProxy,
  TaskStageProxy,
  TaskEnclosureProxy,
  TaskCommentsProxy,
  TaskToDoProxy,

  FlowPageProxy,
  FlowProxy,
  FlowStageProxy,
  FlowGroupListProxy,
  FlowGroupProxy
};
